<template>
    <div>
        <basic-layout title="爱心菜单" :showFilterBtn="true" @handleAdd="handleAdd"
            @handleFilter="$refs.queryPopupRef.show = true">
            <div class="columnFlex">
                <van-row class="listCon">
                    <van-pull-refresh class="pullRefresh" v-model="refreshLoading" @refresh="onRefresh">
                        <van-list v-model="loading" :finished="finished" :finished-text="listData.length ? '没有更多了' : ''"
                            @load="onLoad">
                            <van-swipe-cell v-for="(val, ind) in listData" :key="ind">
                                <div class="cellContent">
                                    <div class="bottom">
                                        <div class="left" @click="handleDetail(val)">
                                            <div>
                                                <p>
                                                    <span class="label">我的建议：</span>
                                                    {{ val.adviceDesc }}
                                                </p>
                                                <p>
                                                    <span class="label">创建时间：</span>
                                                    {{ val.createTime}}
                                                </p>
                                                <p>
                                                    <span class="label">食堂回复：</span>
                                                    <span>{{ val.reply || '-' }}</span>
                                                </p>
                                            </div>
                                        </div>
                                        <div class="right" v-if="val.confirm === '0'">
                                            <van-button type="info" color="#f25454" plain size="mini"
                                                @click="del(val)">删除</van-button>
                                        </div>
                                    </div>
                                </div>
                            </van-swipe-cell>
                        </van-list>
                        <van-empty v-if="!loading && !listData.length" class="custom-image"
                            :image="require('../../../../assets/images/empty.png')" description="暂无数据" />
                    </van-pull-refresh>
                </van-row>
            </div>
        </basic-layout>
        <queryPopup ref="queryPopupRef" @handleSearch="onRefresh" @handleReset="handleReset">
            <van-form class="filterBox">
                <label>食堂列表</label>
                <van-field>
                    <template #input>
                        <div class="textCon" @click="showPicker = true">
                            <span v-if="!canteenText" style="color: #aaa">请选择食堂列表</span>
                            <span v-else>{{ canteenText }}</span>
                        </div>
                    </template>
                </van-field>
                <label>日期</label>
                <van-field>
                    <template #input>
                        <div class="textCon" @click="showStatusDatePicker = true">
                            <span v-if="!applyDateText" style="color: #aaa">请选择日期</span>
                            <span v-else>{{ applyDateText }}</span>
                        </div>
                    </template>
                </van-field>
            </van-form>
        </queryPopup>
        <!-- 预定日期 -->
        <van-calendar v-model="showStatusDatePicker" :allow-same-day="true" :min-date="getMin()" :max-date="getMax()"
            type="range" @confirm="onTimeConfirm" />
        <!-- 食堂列表选择 -->
        <van-action-sheet v-model="showPicker" cancel-text="取消" :actions="canteenList" @select="onConfirm" />
        <!-- 新增/详情/编辑 弹出框 -->
        <fullscreenModal :modal="fullscreenModal" :isAdd="isAdd" label="建议" :isShowEdit="isShowEdit"
            :editType="editType" @handleClose="fullscreenModal = false" @handleEdit="handleEdit"
             @handleCancel="editType = 'detail'" @handleSave="$refs.editFormRef.handleSave()">
            <detail-info v-show="editType == 'detail'" ref="detailRef" />
            <edit-form v-show="editType == 'edit'" @close="fullscreenModal = false" @refresh="onRefresh"
                ref="editFormRef" />
        </fullscreenModal>
        <full-loading v-if="fullLoading"></full-loading>
        <!-- 明细 -->
    </div>
</template>

<script>
    import { getCanteenInfoByLoginUser } from '@/api/srvAssurance/smarcatering/oneWeekMenu';
    import { pageByAdvice, add, del } from '@/api/srvAssurance/smarcatering/myWishMenu';
    import editForm from "./editForm.vue";
    import detailInfo from "./detailInfo.vue";
    export default {
        components: {
            editForm,
            detailInfo,
        },
        data() {
            return {
                showPicker: false,
                canteenList: [],

                isShowEdit: false,
                showStatusDatePicker: false,
                loading: false,
                fullLoading: false,
                refreshLoading: false,
                fullscreenModal: false,
                showStatusPicker: false,
                showCancelModal: false,
                finished: false,
                pageNum: 0,
                pageSize: 20,
                formData: {
                    canteenId: '',
                    startTime: null,
                    endTime: null,
                },
                canteenText: "",
                applyDateText: '',
                listData: [], // 列表数据
                applyStatusList: [], // 申请状态
                isAdd: false,
                editType: "",
                cancelFormData: {
                    bookeId: '', // 取消标识
                    cancelReason: '', // 取消原因
                },
                editParam: {},
            };
        },
        computed: {
            minDate() {
                let nowDate = new Date();
                let array = [];
                let year = nowDate.getFullYear();
                let mon = nowDate.getMonth() + 1;
                for (let i = 0; i < 6; i++) {
                    mon = mon - 1;
                    if (mon <= 0) {
                        year = year - 1;
                        mon = mon + 12;
                    }
                    if (mon < 10) {
                        mon = "0" + mon;
                    }
                    array[i] = year + "-" + mon;
                }

                let day = nowDate.getDate();
                let d = array.pop() + '-' + day;
                return new Date(d);
            }
        },
        // create(){
        //     this.getListData();
        // },
        mounted() {
            this.queryDicData();
        },
        methods: {
            // 新建数据
            handleAdd() {
                this.fullscreenModal = true;
                this.editType = "edit";
                this.isAdd = true;
                this.$refs.editFormRef.resetForm();
            },

            handleDetail(item) {
                this.fullscreenModal = true;
                this.editType = "detail";
                this.isAdd = false;
                this.isShowEdit = false;
                this.editParam = item;
                this.$refs.detailRef.getData(item);
            },
            onConfirm(item) {
                this.formData.canteenId = item.canteenId;
                this.canteenText = item.name;
                this.showPicker = false;
                this.onRefresh();
            },
            getMin() {
                return new Date(this.getDateRange(new Date(), 30, false));
            },
            getMax() {
                return new Date(this.getDateRange(new Date(), 30, true));
            },
            getDateRange(dateNow, intervalDays, bolPastTime) {
                let oneDayTime = 24 * 60 * 60 * 1000;
                let lastDay;

                if (!bolPastTime) {
                    lastDay = new Date(dateNow.getTime() - intervalDays * oneDayTime);
                    return this.formateDate(lastDay);

                } else {
                    lastDay = new Date(dateNow.getTime() + intervalDays * oneDayTime);
                    return this.formateDate(lastDay);

                }
            },
            formateDate(time) {
                let year = time.getFullYear()
                let month = time.getMonth() + 1
                let day = time.getDate()

                if (month < 10) {
                    month = '0' + month
                }

                if (day < 10) {
                    day = '0' + day
                }
                return year + '-' + month + '-' + day + ''
            },
            handleEdit() {
                getInfoById(this.editParam.strangeEatId)
                    .then((res) => {
                        if (res.code === 1000 && res.data) {
                            this.editType = "edit";
                            this.$refs.editFormRef.resetForm();
                            // this.$refs.editFormRef.getData(this.currentParam);
                            this.$refs.editFormRef.getData({ data: res.data });
                            // this.fullscreenModal = true;

                        }
                    })
                    .catch(() => {

                    });

            },

            del(item) {
                let dels = [];
                dels.push(item.wishMenuId)
                this.$Dialog
                    .confirm({
                        title: "是否确定删除该数据？",
                    })
                    .then(async () => {
                        var res = await del({ listIds: dels });
                        if (res.code === 1000) {
                            this.$Notify({ type: "success", message: "操作成功！" });
                            this.onRefresh();
                        }
                    })
                    .catch(() => { });
            },
            // 预定日期选择
            onTimeConfirm(date) {
                const [start, end] = date;
                this.showStatusDatePicker = false;
                this.formData.startTime = new Date(start).format('yyyy-MM-dd');
                this.formData.endTime = new Date(end).format('yyyy-MM-dd');
                this.applyDateText = `${this.formData.startTime} - ${this.formData.endTime}`;
                this.showStatusDatePicker = false;
                this.onRefresh();
            },
            // 获取表格数据
            getListData() {
                if (!this.formData.canteenId) {
                    this.loading = false;
                    this.finished = true;
                    return;
                }
                this.loading = true;
                let params = {
                    ...this.formData,
                    pageNum: this.pageNum,
                    pageSize: this.pageSize,
                }
                pageByAdvice(params).then((res) => {
                    this.loading = false;
                    if (res.code === 1000 && res.data) {
                        this.listData = [...this.listData, ...res.data.records];
                        if (this.listData.length >= res.data.total) {
                            this.finished = true;
                        }
                    } else {
                        this.finished = true;
                    }
                }).catch(() => {
                    this.loading = false;
                    this.finished = true;
                });
            },
            onRefresh() {
                this.refreshLoading = false;
                this.finished = false;
                this.listData = [];
                this.pageNum = 1;
                this.getListData();
            },
            onLoad() {
                this.pageNum++;
                this.getListData();
            },
            // 重置
            handleReset() {
                this.formData.startTime = null;
                this.formData.endTime = null;
                this.pageNum = 1;
                this.pageSize = 20;
                this.applyDateText = "";
                this.onRefresh();
            },
            // 清空查询条件
            clearPicker() {
                this.applyDateText = "";
                this.formData.startTime = null;
                this.formData.endTime = null;
                this.onRefresh();
            },
            // 下拉框数据
            queryDicData() {
                // 食堂列表
                getCanteenInfoByLoginUser().then(res => {
                    if (res.code === 1000 && res.data) {
                        res.data.forEach(item => {
                            item.name = item.canteenName;
                        })
                        this.canteenList = res.data;
                        if (this.canteenList.length) {
                            this.formData.canteenId = this.canteenList[0].canteenId;
                            this.canteenText = this.canteenList[0].canteenName;
                            this.onRefresh();
                        }
                        this.$nextTick(() => {
                            this.$refs.editFormRef.canteenList = this.canteenList;
                        })
                    }
                })
            }
        },
    };
</script>

<style scoped lang="less">
    .listCon {
        .cellContent {
            padding: 0;
            position: relative;

            .top {
                padding: 5px 10px;
                box-sizing: border-box;
                border-bottom: 1px solid #eee;
                position: relative;

                .time {
                    position: absolute;
                    right: 5px;
                    top: 8px;
                    color: #aaa;
                    font-size: 13px;
                }

                .tit {
                    font-size: 15px;
                    height: 30px;

                    b span {
                        font-size: 14px;
                    }

                    .status {
                        background: rgb(70, 172, 240);
                        font-size: 9px;
                        color: #fff;
                        margin-left: 8px;
                        padding: 2px 5px;
                        border-radius: 3px;
                    }

                    .status2 {
                        background: #1aa78f;
                    }

                    .status3 {
                        background: #f25454;
                    }

                    .status4 {
                        background: #b4afaf;
                    }
                }

                .reason {
                    font-size: 13px;
                    color: #aaa;
                    line-height: 20px;
                }

                .type {
                    background: #f2a84e;
                    font-size: 9px;
                    color: #fff;
                    margin-left: 8px;
                    padding: 2px 3px;
                    border-radius: 3px;
                }
            }

            .bottom {
                padding: 5px 10px;
                box-sizing: border-box;
                display: flex;
                overflow: hidden;
                align-items: center;
                border-bottom: 1px solid #eee;

                .left {
                    flex: 1;
                    overflow: hidden;
                }

                .right {
                    margin: 0 5px 0 10px;

                    img {
                        width: 35px;
                        height: 35px;
                    }

                    p {
                        color: #aaa;
                        font-size: 13px;
                    }

                    .van-button {
                        padding: 0 10px;
                        height: 30px;
                        line-height: 30px;
                        border-radius: 17px;
                        font-size: 13px;
                    }
                }
            }

            .flexBtn {
                display: flex;
                justify-content: space-between;

                p {
                    flex: 1;
                    text-align: center;
                    height: 40px;
                    line-height: 40px;
                    color: #4eb3e9;
                    font-size: 15px;
                }

                p:nth-of-type(1) {
                    box-sizing: border-box;
                    border-right: 1px solid #eee;
                    color: #f25454;
                }
            }
        }

     
    }
</style>